package com.xj.api.bussiness.wxLogin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xj.api.bussiness.wxLogin.common.TemplateData;
import com.xj.api.bussiness.wxLogin.service.IWxMessageService;
import com.xj.common.bussiness.wxLogin.entity.WxAccessToken;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Service
public class WxMessageServiceImpl implements IWxMessageService{

    @Override
    public String getMessagePushUrl() {
        if(WxAccessToken.getWxAccessToken().getAccess_token() != null && WxAccessToken.getWxAccessToken().getAccess_token() != ""){
            String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token="+ WxAccessToken.getWxAccessToken().getAccess_token();
            return url;
        }
        return null;
    }

    @Override
    public Map<String, TemplateData> getTempalteDat(String content) {
        JSONArray object = JSONArray.parseArray(content);
        Map<String,TemplateData> map = new HashMap<String,TemplateData>();
        int i = 0;
        for(Iterator it = object.iterator(); it.hasNext();){
            JSONObject jsonObject = (JSONObject)it.next();
            i+=1;
            JSONObject keyword =(JSONObject) jsonObject.get("keyword"+i);
            String value =keyword.get("value")+"";
            String color =keyword.get("color")+"";
            TemplateData templateData = new TemplateData();
            templateData.setValue(value);
            templateData.setColor(color);
            map.put("keyword"+i,templateData);
            System.out.println("keyword>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+map.get("keyword"+i).getValue());
        }
        if(map!= null && map.size()>0){
            return map;
        }
        return null;
    }
}
